package lanbo.cj.lmp.utils;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import lanbo.cj.lmp.BaseApplication;
import lanbo.cj.lmp.Config;

import java.io.*;
import java.util.ArrayList;
import lanbo.cj.lmp.bean.DataLocal;

/**
 * 用于当网络断开等意外情况，造成数据无法同步至后台，先将数据存储至本地
 * Created by white on 15-10-24.
 */
public class LocalData {
    private DataLocal dateLocal;

    /**
     * 保存数据至本地
     */
    public void saveData(Object data) {
        if (dateLocal == null) dateLocal = new DataLocal();
        ArrayList dataList = getLocalData();
        if (dataList == null) dataList = new ArrayList();
        dataList.add(data);
        dateLocal.setDataList(dataList);
        saveLocal(dateLocal);
    }



    /**
     * 将数据保存至本地sd卡中，保证应用退出后数据不会丢失
     */

    public void saveLocal(DataLocal dataLocal) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = BaseApplication.getInstance().openFileOutput(Config.LocalData, Activity.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(dateLocal);
        } catch (Exception e) {
            e.printStackTrace();
            //这里是保存文件产生异常
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    //fos流关闭异常
                    e.printStackTrace();
                }
            }
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    //oos流关闭异常
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 获取本地数据
     */
    public ArrayList getLocalData() {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = BaseApplication.getInstance().openFileInput(Config.LocalData);
            ois = new ObjectInputStream(fis);
            DataLocal dataLocal = (DataLocal) ois.readObject();
            return dataLocal.getDataList();
        } catch (Exception e) {
            e.printStackTrace();
            //这里是读取文件产生异常
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    //fis流关闭异常
                    e.printStackTrace();
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    //ois流关闭异常
                    e.printStackTrace();
                }
            }
        }
        //读取产生异常，返回null
        return null;
    }

    public void clearData(){
        if (dateLocal == null) dateLocal = new DataLocal();
        dateLocal.setDataList(new ArrayList());
        saveLocal(dateLocal);
    }

    /**
     * 清除其中一个数据
     * @param i 要清除数据在队列中的位置
     * */
    public void clearOneData(int i){
        ArrayList list = getLocalData();
        list.remove(i);
        DataLocal dataLocal = new DataLocal();
        dataLocal.setDataList(list);
        saveData(dataLocal);
    }

}
